package cn.iocoder.yudao.module.alipay.service.drawingaccount;

import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.alipay.controller.admin.drawingaccount.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.drawingaccount.DrawingAccountDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.alipay.convert.drawingaccount.DrawingAccountConvert;
import cn.iocoder.yudao.module.alipay.dal.mysql.drawingaccount.DrawingAccountMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.alipay.enums.ErrorCodeConstants.*;

/**
 * 收款账户 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class DrawingAccountServiceImpl implements DrawingAccountService {

    @Resource
    private DrawingAccountMapper drawingAccountMapper;

    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createDrawingAccount(DrawingAccountCreateReqVO createReqVO) {
        // 插入
        DrawingAccountDO drawingAccount = DrawingAccountConvert.INSTANCE.convert(createReqVO);
        drawingAccountMapper.insert(drawingAccount);
        // 返回
        return drawingAccount.getId();
    }

    @Override
    public void updateDrawingAccount(DrawingAccountUpdateReqVO updateReqVO) {
        // 校验存在
        validateDrawingAccountExists(updateReqVO.getId());
        // 更新
        DrawingAccountDO updateObj = DrawingAccountConvert.INSTANCE.convert(updateReqVO);
        drawingAccountMapper.updateById(updateObj);
    }

    @Override
    public void deleteDrawingAccount(Long id) {
        // 校验存在
        validateDrawingAccountExists(id);
        // 删除
        drawingAccountMapper.deleteById(id);
    }

    private void validateDrawingAccountExists(Long id) {
        if (drawingAccountMapper.selectById(id) == null) {
            throw exception(DRAWING_ACCOUNT_NOT_EXISTS);
        }
    }

    @Override
    public DrawingAccountDO getDrawingAccount(Long userId) {
        return drawingAccountMapper.selectById(userId);
    }

    @Override
    public List<DrawingAccountDO> getDrawingAccountList(Collection<Long> ids) {
        return drawingAccountMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DrawingAccountDO> getDrawingAccountPage(DrawingAccountPageReqVO pageReqVO) {
        return drawingAccountMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DrawingAccountDO> getDrawingAccountList(DrawingAccountExportReqVO exportReqVO) {
        return drawingAccountMapper.selectList(exportReqVO);
    }

    @Override
    public DrawingAccountRespVO getByUserId(String userId) {
        AdminUserRespDTO user = adminUserApi.getUser(Long.valueOf(userId));
        DrawingAccountRespVO convert = DrawingAccountConvert.INSTANCE.convert(drawingAccountMapper.getByUserId(userId));
        if(convert!=null){
            convert.setTotalAmount(user.getTotalAmount());
        }
        return convert;
    }

}
